/*
	Created By Max 2017-02-08
 */

!function(a,b){
	"function" == typeof define && (define.amd || define.cmd) ? define(function() {
		return b(a);
	}) : b(a);
}(window,function(a){
	
	function base64(data){
		return window.BASE64.encode(JSON.stringify(data));
	}
	
	A={
		getDateTime:function(e) {
            var t;
            t = e ? new Date(parseInt(e)) : new Date();
            var n = t.getFullYear(),
                a = t.getMonth() + 1,
                r = t.getDate(),
                o = t.getHours(),
                i = t.getMinutes(),
                s = t.getSeconds(),
                c = [];
                a >= 10 || (a = "0" + a), r >= 10 || (r = "0" + r), o >= 10 || (o = "0" + o), i >= 10 || (i = "0" + i), s >= 10 || (s = "0" + s), c[0] = n, c[1] = a, c[2] = r, c[3] = n + "-" + a, c[4] = a + "-" + r, c[5] = o + ":" + i, c[6] = n + "-" + a + "-" + r + " " + o + ":" + i + ":" + s;
            return c;
        },
        getUnixTime:function(e) {
            var t;
            if (e) {
                var n = e.replace(/-/g, "/");
                t = new Date(n)
            } else t = new Date();
            return t.getTime().toString();
        },
        getUrlParam:function(e) {
            var t = new RegExp("(^|&)" + e + "=([^&]*)(&|$)"),
                n = window.location.search.substr(1).match(t);
            return null != n ? unescape(n[2]) : null;
        },
        AJAX:function(url,data,success,closeLoad,closeBase64){
            
            var xhr,index,postData=data.params || {};
            !closeLoad&&(index=layer.open({type: 2,shadeClose:false,shade: 'background-color: rgba(255,255,255,0)'}));

            const error=function(data){
            	data.code=="671" ? Jsborya.dialog({
	                content:"登录已过期，请重新登录",
	                btn:['确定'],
	                code:'671',
	                yes:function(){
	                    console.log(1);
	                }
	            }) : data.code=="2000" ? Jsborya.updateVersion(data.data) : layer.open({
	                content:data.msg||data,
	                skin: "msg",
	                time: 4
	            });
            };
            
            postData.token=data.userInfo.token;
            postData.timestamp=data.userInfo.timestamp;
            postData.applicationID=data.userInfo.applicationId;
            postData.userId=data.userInfo.uid;
            postData = !closeBase64 ? BASE64.encode(JSON.stringify(postData)) : JSON.stringify(postData);
            
            XMLHttpRequest ? xhr=new XMLHttpRequest() : xhr=new ActiveXObject('Microsoft.XMLHTTP');
            xhr.open('POST',url,true);
            xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded;charset=utf-8');
            xhr.setRequestHeader('Accept','application/json,text/javascript;q=0.01');
            xhr.setRequestHeader('AppType','KM_APP');
            xhr.setRequestHeader('boryaAuth',`${data.userInfo.uid},${data.userInfo.appVersion || ''},${data.userInfo.osVersion || ''},3,1`);
            xhr.send(postData);

            xhr.onreadystatechange=function(){
                if(xhr.readyState===4){
                    closeLoad ? typeof closeLoad==='function'&&closeLoad() : layer.close(index);
                    if(xhr.status>=200&&(xhr.status<300 || xhr.status===304)){
                        var responseText=JSON.parse(xhr.responseText);
                        responseText.code=='200' ? success(responseText) : error(responseText);
                    }else{
                        error(xhr.statusText+xhr.status);
                    }
                }
            };
        }
	},
	
	a.Utils=A;
});




window.onload = function() {
	document.body.addEventListener("touchstart", function() {});
};